package com.lk.dao;

import com.lk.beans.User;
import com.lk.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    User user=null;
    //根据用户名查信息
    public User searchByName(String userName){
        try {
            conn = DButils.getConn();
            String sql="select * from user where uname=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,userName);
            rs=ps.executeQuery();
            if(rs.next()){
                user=new User();
                user.setUserid(rs.getInt("uid"));
                user.setUname(userName);
                user.setHead(rs.getString("head"));
                user.setNick(rs.getString("nick"));
                user.setUpwd(rs.getString("upwd"));
                user.setMood(rs.getString("mood"));
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            DButils.close(rs,ps,conn);
        }
        return user;
    }
    //根据用户名查用户信息
    public User queryUserByName(String userName){
        String sql="select * from user where uname=?";
        List<Object> params=new ArrayList<>();
        params.add(userName);
        User user= (User) BaseDao.queryRow(sql,params,User.class);
        return user;
    }

}
